package com.learn.tools;
// Demo: 完整的类定义和使用
public class Car {
    // 字段（属性）
    private String brand;
    private String color;
    private double price;
    private int speed;

    // 静态字段（类变量）
    private static int carCount = 0;

    // 构造方法
    public Car(String brand, String color, double price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
        this.speed = 0;
        carCount++; // 每创建一个对象，计数器加1
    }

    // 默认构造方法
    public Car() {
        this("未知", "黑色", 0.0);
    }

    // 实例方法
    public void accelerate(int increment) {
        speed += increment;
        System.out.println(brand + "加速到" + speed + "km/h");
    }

    public void brake(int decrement) {
        speed = Math.max(0, speed - decrement);
        System.out.println(brand + "减速到" + speed + "km/h");
    }

    public void displayInfo() {
        System.out.printf("品牌: %s, 颜色: %s, 价格: ￥%.2f万, 当前速度: %dkm/h%n",
                brand, color, price, speed);
    }

    // 静态方法
    public static int getCarCount() {
        return carCount;
    }

    // Getter和Setter方法
    public String getBrand() { return brand; }
    public void setBrand(String brand) { this.brand = brand; }

    public String getColor() { return color; }
    public void setColor(String color) { this.color = color; }

    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    public int getSpeed() { return speed; }

    public static double calculateAverage(double... numbers) {
        if (numbers.length == 0) {
            return 0.0;
        }

        double sum = 0;
        for (double num : numbers) {
            sum += num;
        }
        return sum / numbers.length;
    }

    public static void main(String[] args) {
        Car car1 = new Car("Toyota", "白色", 15.8);
        Car car2 = new Car("Honda", "红色", 12.5);

        System.out.println(car1.brand);

        car1.displayInfo();
        car2.displayInfo();

        car1.accelerate(30);
        car2.accelerate(20);
        car1.brake(10);

        calculateAverage(2,3,4);
        System.out.println("总共创建了" + Car.getCarCount() + "辆车");
    }
}